package com.atguigu.day04;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.RestOptions;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author Felix
 * @date 2024/7/12
 * 该案例演示了合流算子_union
 *      可以合并多条流
 *      要求参与合并的多条流数据类型必须要一致
 */
public class Flink05_Union {
    public static void main(String[] args) throws Exception {
        //TODO 1.指定流处理环境
        Configuration conf = new Configuration();
        conf.set(RestOptions.PORT,8088);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        //TODO 2.从指定网络端口8888读取数据
        DataStreamSource<String> ds1 = env.socketTextStream("hadoop102", 8888);
        //TODO 3.从指定网络端口8889读取数据
        DataStreamSource<String> ds2 = env.socketTextStream("hadoop102", 8889);

        //DataStreamSource<Integer> ds3 = env.fromElements(1, 2, 3);
        //TODO 4.合流
        //DataStream<String> unionDS = ds1.union(ds2, ds3);
        DataStream<String> unionDS = ds1.union(ds2);
        //TODO 5.打印输出
        unionDS.print();
        //TODO 6.提交作业
        env.execute();
    }
}
